{% extends "base.html" %}

{% block title %}学员申请 - 乒乓球培训管理系统{% endblock %}

{% block extra_css %}
<style>
    .page-header {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        color: white;
        border-radius: 8px;
        padding: 30px;
        margin-bottom: 30px;
    }

    .stats-cards {
        margin-bottom: 30px;
    }

    .stats-card {
        background: white;
        border-radius: 8px;
        padding: 20px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        text-align: center;
        margin-bottom: 20px;
    }

    .stats-number {
        font-size: 2rem;
        font-weight: bold;
        display: block;
        margin-bottom: 5px;
    }

    .stats-pending { color: #ffc107; }
    .stats-approved { color: #28a745; }
    .stats-rejected { color: #dc3545; }
    .stats-total { color: #007bff; }

    .filters-card {
        background: white;
        border-radius: 8px;
        padding: 20px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        margin-bottom: 20px;
    }

    .application-item {
        background: white;
        border-radius: 8px;
        padding: 20px;
        box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        margin-bottom: 15px;
        border-left: 4px solid #ddd;
        transition: all 0.3s ease;
    }

    .application-item:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 15px rgba(0,0,0,0.15);
    }

    .application-pending {
        border-left-color: #ffc107;
    }

    .application-approved {
        border-left-color: #28a745;
    }

    .application-rejected {
        border-left-color: #dc3545;
    }

    .student-avatar {
        width: 60px;
        height: 60px;
        border-radius: 50%;
        object-fit: cover;
        border: 2px solid #fff;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }

    .status-badge {
        font-size: 0.8rem;
        padding: 4px 12px;
        border-radius: 15px;
        font-weight: 500;
    }

    .status-pending {
        background: #fff3cd;
        color: #856404;
        border: 1px solid #ffeaa7;
    }

    .status-approved {
        background: #d4edda;
        color: #155724;
        border: 1px solid #c3e6cb;
    }

    .status-rejected {
        background: #f8d7da;
        color: #721c24;
        border: 1px solid #f5c6cb;
    }

    .student-info h6 {
        color: #333;
        margin-bottom: 5px;
    }

    .student-meta {
        color: #666;
        font-size: 0.9rem;
        margin-bottom: 10px;
    }

    .application-note {
        background: #f8f9fa;
        border-radius: 6px;
        padding: 12px;
        margin-top: 10px;
        font-size: 0.9rem;
        color: #333;
    }

    .note-label {
        font-weight: 600;
        color: #666;
        margin-bottom: 5px;
    }

    .application-dates {
        background: #f8f9fa;
        border-radius: 6px;
        padding: 10px;
        margin-top: 10px;
        font-size: 0.85rem;
    }

    .date-item {
        margin-bottom: 5px;
    }

    .date-item:last-child {
        margin-bottom: 0;
    }

    .date-label {
        font-weight: 600;
        color: #666;
        display: inline-block;
        width: 80px;
    }

    .quick-actions {
        display: flex;
        gap: 8px;
        margin-top: 10px;
    }

    .btn-action {
        padding: 6px 12px;
        border-radius: 15px;
        font-size: 0.8rem;
        border: none;
    }

    .btn-approve {
        background: #28a745;
        color: white;
    }

    .btn-approve:hover {
        background: #218838;
        color: white;
    }

    .btn-reject {
        background: #dc3545;
        color: white;
    }

    .btn-reject:hover {
        background: #c82333;
        color: white;
    }

    .btn-detail {
        background: #007bff;
        color: white;
    }

    .btn-detail:hover {
        background: #0056b3;
        color: white;
    }

    .empty-state {
        text-align: center;
        padding: 80px 20px;
        color: #666;
    }

    .empty-state i {
        font-size: 5rem;
        color: #ddd;
        margin-bottom: 20px;
    }

    .filter-buttons .btn {
        margin: 3px;
        border-radius: 20px;
    }

    .batch-actions {
        background: #e9ecef;
        border-radius: 8px;
        padding: 15px;
        margin-bottom: 20px;
        display: none;
    }

    .timeline-icon {
        display: inline-block;
        width: 20px;
        text-align: center;
        margin-right: 8px;
    }

    .select-all-wrapper {
        margin-bottom: 15px;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- 页面头部 -->
    <div class="row">
        <div class="col-12">
            <div class="page-header">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h2 class="mb-2">学员申请管理</h2>
                        <p class="mb-0 fs-5">审核和管理学员的选择申请</p>
                    </div>
                    <div>
                        <a href="{{ url_for('coach_student.my_students') }}" class="btn btn-light">
                            <i class="bi bi-people me-2"></i>我的学员
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 统计卡片 -->
    <div class="row stats-cards">
        <div class="col-md-3">
            <div class="stats-card">
                <span class="stats-number stats-total">{{ total }}</span>
                <span>总申请数</span>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stats-card">
                <span class="stats-number stats-pending">
                    {{ applications|selectattr('status', 'equalto', 'pending')|list|length if applications else 0 }}
                </span>
                <span>待审核</span>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stats-card">
                <span class="stats-number stats-approved">
                    {{ applications|selectattr('status', 'equalto', 'approved')|list|length if applications else 0 }}
                </span>
                <span>已通过</span>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stats-card">
                <span class="stats-number stats-rejected">
                    {{ applications|selectattr('status', 'equalto', 'rejected')|list|length if applications else 0 }}
                </span>
                <span>已拒绝</span>
            </div>
        </div>
    </div>

    <!-- 筛选器 -->
    <div class="row">
        <div class="col-12">
            <div class="filters-card">
                <div class="d-flex justify-content-between align-items-center">
                    <div>
                        <h6 class="mb-2">状态筛选</h6>
                        <div class="filter-buttons">
                            <a href="{{ url_for('coach_student.applications') }}"
                               class="btn btn-sm {{ 'btn-primary' if not status else 'btn-outline-primary' }}">
                                全部
                            </a>
                            <a href="{{ url_for('coach_student.applications', status='pending') }}"
                               class="btn btn-sm {{ 'btn-warning' if status == 'pending' else 'btn-outline-warning' }}">
                                待审核
                            </a>
                            <a href="{{ url_for('coach_student.applications', status='approved') }}"
                               class="btn btn-sm {{ 'btn-success' if status == 'approved' else 'btn-outline-success' }}">
                                已通过
                            </a>
                            <a href="{{ url_for('coach_student.applications', status='rejected') }}"
                               class="btn btn-sm {{ 'btn-danger' if status == 'rejected' else 'btn-outline-danger' }}">
                                已拒绝
                            </a>
                        </div>
                    </div>
                    <div class="text-muted">
                        共 {{ total }} 条记录
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 批量操作 -->
    {% if applications and status == 'pending' %}
    <div class="row">
        <div class="col-12">
            <div class="batch-actions" id="batchActions">
                <form method="POST" action="{{ url_for('coach_student.batch_approve') }}" id="batchForm">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <span class="fw-bold">已选择 <span id="selectedCount">0</span> 个申请</span>
                        </div>
                        <div>
                            <button type="button" class="btn btn-success btn-sm me-2" onclick="batchAction('approve')">
                                <i class="bi bi-check-circle me-1"></i>批量通过
                            </button>
                            <button type="button" class="btn btn-danger btn-sm me-2" onclick="batchAction('reject')">
                                <i class="bi bi-x-circle me-1"></i>批量拒绝
                            </button>
                            <button type="button" class="btn btn-secondary btn-sm" onclick="clearSelection()">
                                <i class="bi bi-x-lg me-1"></i>取消选择
                            </button>
                        </div>
                    </div>
                    <input type="hidden" name="action" id="batchActionInput">
                </form>
            </div>
        </div>
    </div>
    {% endif %}

    <!-- 申请列表 -->
    {% if applications %}
    <div class="row">
        <div class="col-12">
            {% if status == 'pending' %}
            <div class="select-all-wrapper">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" id="selectAll">
                    <label class="form-check-label" for="selectAll">
                        全选
                    </label>
                </div>
            </div>
            {% endif %}

            {% for application in applications %}
            <div class="application-item application-{{ application.status }}">
                <div class="row align-items-start">
                    {% if status == 'pending' %}
                    <div class="col-md-1">
                        <div class="form-check">
                            <input class="form-check-input application-checkbox" type="checkbox"
                                   value="{{ application.id }}" name="relation_ids">
                        </div>
                    </div>
                    {% endif %}
                    <div class="col-md-1">
                        <img src="{{ application.student.photo_url or url_for('static', filename='images/default-avatar.svg') }}"
                             alt="{{ application.student.real_name }}" class="student-avatar">
                    </div>
                    <div class="col-md-{{ '5' if status == 'pending' else '6' }}">
                        <div class="student-info">
                            <div class="d-flex justify-content-between align-items-start mb-2">
                                <h6>{{ application.student.real_name }}</h6>
                                <span class="status-badge status-{{ application.status }}">
                                    {% if application.status == 'pending' %}
                                        <i class="bi bi-clock me-1"></i>待审核
                                    {% elif application.status == 'approved' %}
                                        <i class="bi bi-check-circle me-1"></i>已通过
                                    {% elif application.status == 'rejected' %}
                                        <i class="bi bi-x-circle me-1"></i>已拒绝
                                    {% endif %}
                                </span>
                            </div>

                            <div class="student-meta">
                                <span class="me-3">
                                    <i class="bi bi-person me-1"></i>
                                    {{ '男' if application.student.gender == 'male' else '女' }}
                                    {% if application.student.age %}・{{ application.student.age }}岁{% endif %}
                                </span>
                                {% if application.student.phone %}
                                <span class="me-3">
                                    <i class="bi bi-telephone me-1"></i>{{ application.student.phone }}
                                </span>
                                {% endif %}
                                <span>
                                    <i class="bi bi-building me-1"></i>{{ application.student.campus.name if application.student.campus }}
                                </span>
                            </div>

                            {% if application.apply_note %}
                            <div class="application-note">
                                <div class="note-label">申请说明：</div>
                                {{ application.apply_note[:150] }}{% if application.apply_note|length > 150 %}...{% endif %}
                            </div>
                            {% endif %}

                            {% if application.response_note %}
                            <div class="application-note" style="background: #e7f3ff; border: 1px solid #b3d7ff;">
                                <div class="note-label">
                                    <i class="bi bi-chat-dots me-1"></i>我的回复：
                                </div>
                                {{ application.response_note }}
                            </div>
                            {% endif %}
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="application-dates">
                            <div class="date-item">
                                <span class="timeline-icon"><i class="bi bi-send text-primary"></i></span>
                                <span class="date-label">申请时间：</span>
                                {{ application.applied_at.strftime('%m月%d日 %H:%M') if application.applied_at }}
                            </div>
                            {% if application.responded_at %}
                            <div class="date-item">
                                <span class="timeline-icon">
                                    <i class="bi bi-{{ 'check' if application.status == 'approved' else 'x' }}-circle
                                       text-{{ 'success' if application.status == 'approved' else 'danger' }}"></i>
                                </span>
                                <span class="date-label">处理时间：</span>
                                {{ application.responded_at.strftime('%m月%d日 %H:%M') }}
                            </div>
                            {% endif %}
                        </div>
                    </div>
                    <div class="col-md-2 text-end">
                        <div class="quick-actions">
                            {% if application.status == 'pending' %}
                            <a href="{{ url_for('coach_student.approve', relation_id=application.id) }}"
                               class="btn btn-action btn-approve" title="审核申请">
                                <i class="bi bi-check me-1"></i>审核
                            </a>
                            {% elif application.status == 'approved' %}
                            <a href="{{ url_for('coach_student.student_detail', student_id=application.student.id) }}"
                               class="btn btn-action btn-detail" title="查看学员">
                                <i class="bi bi-eye me-1"></i>详情
                            </a>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
    </div>

    <!-- 分页 -->
    {% if pagination and pagination.pages > 1 %}
    <div class="row">
        <div class="col-12">
            <nav aria-label="申请列表分页">
                <ul class="pagination justify-content-center">
                    {% if pagination.has_prev %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('coach_student.applications', page=pagination.prev_num, status=status) }}">
                            <i class="bi bi-chevron-left"></i>
                        </a>
                    </li>
                    {% endif %}

                    {% for page_num in pagination.iter_pages() %}
                        {% if page_num %}
                            {% if page_num != pagination.page %}
                            <li class="page-item">
                                <a class="page-link" href="{{ url_for('coach_student.applications', page=page_num, status=status) }}">
                                    {{ page_num }}
                                </a>
                            </li>
                            {% else %}
                            <li class="page-item active">
                                <span class="page-link">{{ page_num }}</span>
                            </li>
                            {% endif %}
                        {% else %}
                        <li class="page-item disabled">
                            <span class="page-link">…</span>
                        </li>
                        {% endif %}
                    {% endfor %}

                    {% if pagination.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('coach_student.applications', page=pagination.next_num, status=status) }}">
                            <i class="bi bi-chevron-right"></i>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
        </div>
    </div>
    {% endif %}

    {% else %}
    <!-- 空状态 -->
    <div class="row">
        <div class="col-12">
            <div class="empty-state">
                <i class="bi bi-inbox"></i>
                {% if status %}
                <h4>暂无{{ {'pending': '待审核', 'approved': '已通过', 'rejected': '已拒绝'}[status] }}的申请</h4>
                <p class="text-muted">您当前没有{{ {'pending': '需要处理', 'approved': '已通过', 'rejected': '已拒绝'}[status] }}的学员申请</p>
                {% else %}
                <h4>还没有学员申请</h4>
                <p class="text-muted">目前还没有学员向您发送选择申请</p>
                {% endif %}
                <a href="{{ url_for('coach_student.applications') }}" class="btn btn-primary">
                    <i class="bi bi-arrow-clockwise me-2"></i>刷新页面
                </a>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 应用项悬停效果
    $('.application-item').hover(
        function() {
            $(this).addClass('shadow');
        },
        function() {
            $(this).removeClass('shadow');
        }
    );

    // 全选功能
    $('#selectAll').change(function() {
        $('.application-checkbox').prop('checked', $(this).is(':checked'));
        updateBatchActions();
    });

    // 单项选择
    $('.application-checkbox').change(function() {
        updateBatchActions();
    });

    // 更新批量操作显示
    function updateBatchActions() {
        const checkedCount = $('.application-checkbox:checked').length;
        const totalCount = $('.application-checkbox').length;

        $('#selectedCount').text(checkedCount);

        if (checkedCount > 0) {
            $('#batchActions').fadeIn();
        } else {
            $('#batchActions').fadeOut();
        }

        // 更新全选状态
        if (checkedCount === totalCount && totalCount > 0) {
            $('#selectAll').prop('checked', true).prop('indeterminate', false);
        } else if (checkedCount > 0) {
            $('#selectAll').prop('checked', false).prop('indeterminate', true);
        } else {
            $('#selectAll').prop('checked', false).prop('indeterminate', false);
        }
    }

    // 初始化批量操作状态
    updateBatchActions();
});

function batchAction(action) {
    const checkedItems = $('.application-checkbox:checked');

    if (checkedItems.length === 0) {
        alert('请先选择要处理的申请');
        return;
    }

    const actionText = action === 'approve' ? '通过' : '拒绝';
    const confirmMessage = `确认要批量${actionText}所选的 ${checkedItems.length} 个申请吗？`;

    if (confirm(confirmMessage)) {
        $('#batchActionInput').val(action);
        $('#batchForm').submit();
    }
}

function clearSelection() {
    $('.application-checkbox').prop('checked', false);
    $('#selectAll').prop('checked', false).prop('indeterminate', false);
    $('#batchActions').fadeOut();
}
</script>
{% endblock %}